package com.lxl.controller;

import com.lxl.entity.PageResult;
import com.lxl.pojo.Student;
import com.lxl.service.StudentService;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.xml.transform.Result;
import javax.xml.ws.RequestWrapper;

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/allStudents")
    public ModelAndView allStudents(){
        int pageNum=1;
        int pageSize=20;
        PageResult pageResult=studentService.allStudents(pageNum,pageSize);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("stus");
        modelAndView.addObject("stus",pageResult);
        System.out.println(pageResult.getRows().toString());
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/addStudent")
    public ModelAndView addStudent(Student student){
        ModelAndView modelAndView=new ModelAndView();
        System.out.println(student.toString());
        try {
            Integer count=studentService.addStudent(student);
            if(count!=1){
                System.out.println("添加失败！");
                modelAndView.setViewName("main");
                return modelAndView;
            }
            System.out.println("添加成功！");
            modelAndView.setViewName("main");
            return modelAndView;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("添加异常！");
            modelAndView.setViewName("404");
            return modelAndView;
        }
    }

    @RequestMapping("/add")
    public String add(){
        return "addStu";
    }

    @RequestMapping("/delStudent")
    public String delStudent(String stu_id){
        try {
            Integer count=studentService.delStudent(stu_id);
            if(count!=1){
                System.out.println("删除失败！");
                return "main";
            }
            System.out.println("删除成功！");
            return "main";
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("删除异常！");
            return "main";
        }
    }

    @RequestMapping("/editStudent")
    public String editStudent(Student student){
        try {
            Integer count=studentService.editStudent(student);
            if(count!=1){
                System.out.println("修改失败！");
                return "main";
            }
            System.out.println("修改成功！");
            return "main";
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("系统异常");
            return "main";
        }
    }
}
